//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "DiscretizedDiscountBond.h"
using namespace Cephei::QL;
#include <gen/QL/Lattice.h>
#include <gen/QL/Math/Array.h>
#include <gen/QL/DiscretizedAsset.h>
using namespace Cephei::QL::Math;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::CDiscretizedDiscountBond::CDiscretizedDiscountBond () : CDiscretizedAsset(CDiscretizedDiscountBond::typeid)
{
    try
    {
#ifdef HANDLE
        _phDiscretizedDiscountBond = NULL;
#endif
        _ppDiscretizedDiscountBond = new boost::shared_ptr<QuantLib::DiscretizedDiscountBond> (new QuantLib::DiscretizedDiscountBond ( ));
        SetDiscretizedAsset (boost::dynamic_pointer_cast<QuantLib::DiscretizedAsset> (*_ppDiscretizedDiscountBond));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::CDiscretizedDiscountBond::CDiscretizedDiscountBond (boost::shared_ptr<QuantLib::DiscretizedDiscountBond>& childNative, Object^ owner) : CDiscretizedAsset(CDiscretizedDiscountBond::typeid)
{
#ifdef HANDLE
	_phDiscretizedDiscountBond = NULL;
#endif
	_ppDiscretizedDiscountBond = &childNative;
    _ppDiscretizedAsset = new boost::shared_ptr<QuantLib::DiscretizedAsset> (boost::dynamic_pointer_cast<QuantLib::DiscretizedAsset> (*_ppDiscretizedDiscountBond));
}
Cephei::QL::CDiscretizedDiscountBond::CDiscretizedDiscountBond (QuantLib::DiscretizedDiscountBond& childNative, Object^ owner) : CDiscretizedAsset(CDiscretizedDiscountBond::typeid)
{
#ifdef HANDLE
	_phDiscretizedDiscountBond = NULL;
#endif
	_ppDiscretizedDiscountBond = new boost::shared_ptr<QuantLib::DiscretizedDiscountBond> (&childNative);
    _ppDiscretizedAsset = new boost::shared_ptr<QuantLib::DiscretizedAsset> (boost::dynamic_pointer_cast<QuantLib::DiscretizedAsset> (*_ppDiscretizedDiscountBond));
    _DiscretizedDiscountBondOwner = owner;
    _DiscretizedAssetOwner = owner;
}

Cephei::QL::CDiscretizedDiscountBond::CDiscretizedDiscountBond (CDiscretizedDiscountBond^ copy) : CDiscretizedAsset(CDiscretizedDiscountBond::typeid)
{
#ifdef HANDLE
	_phDiscretizedDiscountBond = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppDiscretizedDiscountBond = new boost::shared_ptr<QuantLib::DiscretizedDiscountBond> (copy->GetShared());
        _ppDiscretizedAsset = new boost::shared_ptr<QuantLib::DiscretizedAsset> (boost::dynamic_pointer_cast<QuantLib::DiscretizedAsset> (*_ppDiscretizedDiscountBond));
    }
}
Cephei::QL::CDiscretizedDiscountBond::CDiscretizedDiscountBond (System::Type^ t) : CDiscretizedAsset(CDiscretizedDiscountBond::typeid)
{
#ifdef HANDLE
	_phDiscretizedDiscountBond = NULL;
#endif
	if (!t->IsSubclassOf(CDiscretizedDiscountBond::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::CDiscretizedDiscountBond::CDiscretizedDiscountBond (QuantLib::Handle<QuantLib::DiscretizedDiscountBond>& childNative, Object^ owner)  : CDiscretizedAsset(CDiscretizedDiscountBond::typeid)
{
	_phDiscretizedDiscountBond = &childNative;
	_ppDiscretizedDiscountBond = &static_cast<boost::shared_ptr<QuantLib::DiscretizedDiscountBond>>(childNative.currentLink());
    _ppDiscretizedAsset = new boost::shared_ptr<QuantLib::DiscretizedAsset> (boost::dynamic_pointer_cast<QuantLib::DiscretizedAsset> (*_ppDiscretizedDiscountBond));
    _DiscretizedDiscountBondOwner = owner;
}
Cephei::QL::CDiscretizedDiscountBond::CDiscretizedDiscountBond (QuantLib::Handle<QuantLib::DiscretizedDiscountBond> childNative)  : CDiscretizedAsset(CDiscretizedDiscountBond::typeid)
{
	_phDiscretizedDiscountBond = &childNative;
	_ppDiscretizedDiscountBond = &static_cast<boost::shared_ptr<QuantLib::DiscretizedDiscountBond>>(childNative.currentLink());
    _ppDiscretizedAsset = new boost::shared_ptr<QuantLib::DiscretizedAsset> (boost::dynamic_pointer_cast<QuantLib::DiscretizedAsset> (*_ppDiscretizedDiscountBond));
}
#endif
#ifdef STRUCT
Cephei::QL::CDiscretizedDiscountBond::CDiscretizedDiscountBond (QuantLib::DiscretizedDiscountBond childNative)  : CDiscretizedAsset(CDiscretizedDiscountBond::typeid)
{
#ifdef HANDLE
	_phDiscretizedDiscountBond = NULL;
#endif
	_ppDiscretizedDiscountBond = new boost::shared_ptr<QuantLib::DiscretizedDiscountBond> (new QuantLib::DiscretizedDiscountBond (childNative));
    _ppDiscretizedAsset = new boost::shared_ptr<QuantLib::DiscretizedAsset> (boost::dynamic_pointer_cast<QuantLib::DiscretizedAsset> (*_ppDiscretizedDiscountBond));
}
#endif

Cephei::QL::CDiscretizedDiscountBond::~CDiscretizedDiscountBond ()
{
    if (_ppDiscretizedDiscountBond != NULL)
    {
	    delete _ppDiscretizedDiscountBond;
        _ppDiscretizedDiscountBond = NULL;
    }
}
Cephei::QL::CDiscretizedDiscountBond::!CDiscretizedDiscountBond ()
{
    if (_ppDiscretizedDiscountBond != NULL)
    {
	    delete _ppDiscretizedDiscountBond;
    }
}
QuantLib::DiscretizedDiscountBond& Cephei::QL::CDiscretizedDiscountBond::GetReference ()
{
    if (_ppDiscretizedDiscountBond == NULL) throw gcnew NativeNullException ();
	return **_ppDiscretizedDiscountBond;
}
boost::shared_ptr<QuantLib::DiscretizedDiscountBond>& Cephei::QL::CDiscretizedDiscountBond::GetShared ()
{
    if (_ppDiscretizedDiscountBond == NULL) throw gcnew NativeNullException ();
	return *_ppDiscretizedDiscountBond;
}
QuantLib::DiscretizedDiscountBond* Cephei::QL::CDiscretizedDiscountBond::GetPointer ()
{
    if (_ppDiscretizedDiscountBond == NULL) throw gcnew NativeNullException ();
	return &**_ppDiscretizedDiscountBond;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::DiscretizedDiscountBond>& Cephei::QL::CDiscretizedDiscountBond::GetHandle ()
{
	if (_phDiscretizedDiscountBond == NULL)
	{
		_phDiscretizedDiscountBond = new Handle<QuantLib::DiscretizedDiscountBond> (*_ppDiscretizedDiscountBond);
	}
	return *_phDiscretizedDiscountBond;
}
#endif
bool Cephei::QL::CDiscretizedDiscountBond::HasNative () 
{
	return (_ppDiscretizedDiscountBond != NULL);
}

Cephei::IVector<Double>^ Cephei::QL::CDiscretizedDiscountBond::MandatoryTimes::get ()
{
    try
    {
    	std::vector<QuantLib::Time> _rv = (std::vector<QuantLib::Time>)(*_ppDiscretizedDiscountBond)->mandatoryTimes ( );   
        Cephei::IVector<Double>^ _nrv = gcnew CoVector<Double> (gcnew CDoubleVector (_rv));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::IDiscretizedDiscountBond^ Cephei::QL::CDiscretizedDiscountBond::Reset (UInt64 size)
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        QuantLib::Size _size = (QuantLib::Size)ValueHelper::Convert (size);
    	(*_ppDiscretizedDiscountBond)->reset ( _size );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::IDiscretizedDiscountBond^ Cephei::QL::CDiscretizedDiscountBond_Factory::Create ()
{
    return gcnew CDiscretizedDiscountBond ();
}
